/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: main.cpp
 *
 * Purpose: main test app implementation
 *
 * Developer:
 *   wen.gu , 2021-09-17
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "arg_parser.h"




/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/


int main(int argc, char* argv[])
{
    icpp::core::ArgParser a;
    // add specified type of variable.
    // 1st argument is long name
    // 2nd argument is short name (no short name if '\0' specified)
    // 3rd argument is description
    // 4th argument is mandatory (optional. default is false)
    // 5th argument is default value  (optional. it used when mandatory is false)
    std::string host_str;
    int port_num;
    std::string protocol_type;
    bool got_gzip;

    a.addWithParamNoneHandler("help",'?', "show this usage info", [&a]() -> icpp::core::ArgumentParseError
    {
        a.showHelp();
        return icpp::core::ArgumentParseError::kDoCommand; /** the help info need return a specail error code.*/
    });

    a.addWithParamNoneHandler("protocols",ARG_NO_SHORT_NAME, "show useable protocol types", []() -> icpp::core::ArgumentParseError
    {
        std::cout << "http,\nhttps,\nssh,\nftp"<< std::endl;
        return icpp::core::ArgumentParseError::kDoCommand; /** if current argument just for do something, here must return this error code */
    });

    a.addWithParamRequired<std::string>("host", 'h', "host name", true, host_str);

    // 6th argument is extra constraint.
    // Here, port number must be 1 to 65535 by cmdline::range().
    a.addWithParamRequired<int>("port", 'p', "port number", false, port_num, 80, icpp::core::ParamRange(1, 65535));

    // cmdline::oneof() can restrict options.
    a.addWithParamRequired<std::string>("type", 't', "protocol type", false, protocol_type, "http", icpp::core::ParamOneOf<std::string>({"http", "https", "ssh", "ftp"}));

    // Boolean flags also can be defined.
    // Call add method without a type parameter.
    a.addWithParamNone("gzip", ARG_NO_SHORT_NAME, "gzip when transfer", got_gzip);

    // Run parser.
    // It returns only if command line arguments are valid.
    // If arguments are invalid, a parser output error msgs then exit program.
    // If help flag ('--help' or '-?') is specified, a parser output usage message then exit program.
    icpp::core::ArgumentParseError ret = a.onParse(argc, argv);

    if (icpp::core::ArgumentParseError::kSuccess == ret)
    {
        std::cout << "parse command line argument successful" << std::endl;
        std::cout << "host: " << host_str << std::endl;
        std::cout << "port_num: " << port_num << std::endl;
        std::cout << "protocol: " << protocol_type << std::endl;
        std::cout << "is got gzip: " << std::endl;
    }
    else
    {
        if (ret != icpp::core::ArgumentParseError::kDoCommand)
        {
            std::cout << "error: " << a.error() << std::endl;
            a.showHelp();
        }

    }

    return 0;
}
